<template>
  <div class="box">
    <Card>
      <span slot="title">商品列表</span>
      <div slot="title1">
        <!-- 表格 -->
        <el-table height="650px" :data="tableData" style="width: 100%">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">

                <el-form-item label="商品ID" disabled>
                  <span>{{ props.row.id }}</span>
                </el-form-item>

                <el-form-item label="商品名称">
                  <span>{{ props.row.name }}</span>
                </el-form-item>

                <el-form-item label="商品分类">
                  <span>{{ props.row.category }}</span>
                </el-form-item>

                <el-form-item label="商品价格">
                  <span>{{ props.row.price }}</span>
                </el-form-item>

                <el-form-item label="商品图片">
                  <img :src="urimg+props.row.imgUrl" alt="" />
                </el-form-item>

                <el-form-item label="创建时间">
                  <span>{{ props.row.ctime }}</span>
                </el-form-item>
                <el-form-item label="商品评价">
                  <span>{{ props.row.rating }}</span>
                </el-form-item>
                <el-form-item label="商品销量">
                  <span>{{ props.row.sellCount }}</span>
                </el-form-item>
                <el-form-item label="商品描述">
                  <span>{{ props.row.goodsDesc }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" prop="id"> </el-table-column>
          <el-table-column label="所属分类" prop="name"> </el-table-column>
          <el-table-column label="商品价格" prop="price"> </el-table-column>
          <el-table-column label="商品图片">
            <!-- 引入插槽 -->
            <template slot-scope="scope">
              <img :src="urimg + scope.row.imgUrl" alt="" />
            </template>
          </el-table-column>
          <el-table-column label="商品描述" prop="sellCount"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button @click="bianji(scope.row)" size="mini">编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <!-- 分业器 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="curpage"
          :page-size="papage"
          :page-sizes="[1, 5, 10, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
     
        <!-- 编辑的弹出框 -->
        <el-dialog title="修改信息" :visible.sync="isshow">
          <el-form :model="openabs">
            <el-form-item label="商品名称" label-width="100px">
              <el-input v-model="openabs.name" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="商品分类" label-width="100px">
              <el-input v-model="openabs.category" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="商品价格" label-width="100px">
              <el-input v-model="openabs.price" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="商品图片地址" label-width="100px">
              <el-upload
                class="avatar-uploader"
                action="http://127.0.0.1:5000/goods/goods_img_upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="urimg + imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>

            <el-form-item label="商品描述" label-width="100px">
              <el-input v-model="openabs.goodsDesc" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="商品ID" label-width="100px" >
              <el-input disabled v-model="openabs.id" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="isshow = false">取 消</el-button>
            <el-button type="primary" @click="queding">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </Card>
  </div>
</template>

<script>
//添加分类接口
import { apigoodshq_js, apigoodssc_js, apigoodsxg_js } from "@/utils/api.js";

import Card from "@/components/Card.vue";
export default {
  components: {
    Card,
  },

  data() {
    return {
      urimg: "http://127.0.0.1:5000/upload/imgs/goods_img/",
      tableData: [],
      //页数
      curpage: 1,
      //条数
      papage: 10,
      //总条数
      total: 200,
      //接口
      isshow: false,

      form: "",
      imageUrl: "",
      openabs: {
        name: "",
        category: "",
        price: "",
        goodsDesc: "",
        id: "",
      },
    };
  },
  methods: {
    async getlist() {
      let { data, total } = await apigoodshq_js({
        currentPage: this.curpage,
        pageSize: this.papage,
      });

      this.total = total;

      this.tableData = data;

      // 边界判断  当一个页面没有值的时候 就主动减少一页 但是页数不能少于1
      if (!data.length && this.curpage > 0) {
        this.curpage--;
        this.getlist();
      }
    },
    //确定发送参数
    queding() {
      this.isshow = false;
      apigoodsxg_js({
        name: this.openabs.name,
        category: this.openabs.category,
        price: this.openabs.price,
        imgUrl: this.imageUrl,
        goodsDesc: this.openabs.goodsDesc,
        id: this.openabs.id,
      });
      //重绘
      this.getlist()
    },
    //发送成功之后
    handleAvatarSuccess(res) {
      let { imgUrl } = res;
      this.imageUrl = imgUrl;
      
    },
    //发送成功之前
    beforeAvatarUpload() {},

    //编辑
    bianji(row) {
      this.openabs = row
      this.isshow = true;
      this.imageUrl = row.imgUrl;
    },

    //删除
    async handleDelete(row) {
      await apigoodssc_js({
        id: row.id,
      });
      //重绘
      this.getlist();
    },

    //条数
    handleSizeChange(val) {
      this.papage = val;
      this.getlist();
    },
    //条数
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.curpage = val;
      this.getlist();
    },
  },

  created() {
    this.getlist();
  },
};
</script>

<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
img {
  width: 50px;
  height: 50px;
}
/* 图片框的大小 */
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>